import axios from 'axios'


// 创建axios 实例
const service = axios.create({
    baseURL: 'http://en.test.1demo.cc', // url = baseURL + request url
    // withCredentials: true, // 跨域请求时发送cookie
    timeout: 10000 // 请求超时时间
})

// request interceptor
service.interceptors.request.use(
    config => {
        config.headers['token'] = localStorage.getItem('token')
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

// response interceptor
service.interceptors.response.use(
    response => {
        const res = response.data
        if (res.code !== 200) {
            return Promise.reject(new Error(res.msg || 'Error'))
        } else {
            return res
        }
    },
    error => {
        return Promise.reject(error)
    }
)

export default service
